package com.itheima.tanhua.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.tanhua.api.mongo.TanhuaServiceApi;
import com.itheima.tanhuan.pojo.mongo.RecommendUser;
import com.itheima.tanhuan.pojo.mongo.UserLike;
import com.itheima.tanhuan.pojo.mongo.Visitors;
import com.mongodb.client.result.UpdateResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@DubboService
public class TanhuaServiceApiImpl implements TanhuaServiceApi {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findFateValue(Long uid, Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId).and("toUserId").is(uid));
        RecommendUser templateOne = mongoTemplate.findOne(query, RecommendUser.class);

        Long score = templateOne.getScore();
        if (score==null){
            score=60l;
        }
        templateOne.setScore(score);
        return templateOne;
    }
    //1.获得当前登录者id,已经喜欢的用户
    @Override
    public List<Long> findLikeUser(Long uid) {
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(uid)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        return likeUserIds;
    }
    //2.找到推荐用户的id,排除以喜欢的用户
    @Override
    public List<Long> findRecommentUserId(Long uid, List<Long> findLikeUserIds) {
        Query query=new Query();
        if (ObjectUtil.isNotEmpty(findLikeUserIds)||findLikeUserIds.size()!=0){
            query = Query.query(Criteria.where("toUserId").is(uid).and("toUserId").nin(findLikeUserIds));
        }else {
            query = Query.query(Criteria.where("toUserId").is(uid));
        }
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        List<Long> userIds = CollUtil.getFieldValues(recommendUsers, "userId", Long.class);
        return userIds;
    }
    //保存喜欢记录，有则更新，无则创建
    @Override
    public Boolean saveUserLike(Long uid, Long likeUserId, boolean isUserLike) {
        Query query=Query.query(Criteria.where("userId").is(uid).and("likeUserId").is(likeUserId));
        Update update=new Update();
        update.set("isLike",isUserLike);
        update.set("updated",System.currentTimeMillis());
        Boolean isUserLike1 = haveIsUserLike(uid, likeUserId);
        if (!isUserLike1){update.set("created",System.currentTimeMillis());}
        UpdateResult user_like = mongoTemplate.upsert(query, update, UserLike.class, "user_like");
        return true;
    }

    @Override
    public Boolean isDoubleLike(Long uid, Long likeUserId) {
        return haveIsUserLike(uid,likeUserId)&&haveIsUserLike(likeUserId,uid);
    }
//记录来访者信息
    @Override
    public void upset(Long visitor, Long userId, Long score) {
       //判断当前有没有来访信息
        Query query=Query.query(Criteria.where("userId").is(userId)
        .and("visitorUserId").is(visitor));
        boolean exists = mongoTemplate.exists(query, Visitors.class);
        Update update=new Update();
        //存在和不存在都要执行
        Long timeNow = System.currentTimeMillis();
        update.set("date",timeNow);
        Date date=new Date(timeNow);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(date);
        update.set("visitDate",format);
        //不存在时执行
        if (!exists){
            update.set("score",score);
            update.set("from","首页");
        }
        mongoTemplate.upsert(query,update,Visitors.class);

    }


    public Boolean haveIsUserLike(Long uid,Long likeUserId){
        return mongoTemplate.exists(Query.query(Criteria.where("userId").is(uid)
        .and("likeUserId").is(likeUserId)),UserLike.class);
    }



}
